﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Pathology.Control;
using PathologyBLL.Service;
using PathologyDAL.Modle;
using Sunny.UI;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Pathology.UI
{
    public partial class Query : UIPage
    {
        public Query()
        {
            InitializeComponent();
        }

        private QueryServices services;

        private QueryItem queryItem;

        private void Query_Initialize(object sender, EventArgs e)
        {


            //初始化

            services = new QueryServices();
            queryItem = new QueryItem();
            checkedConditions=new List<int> {};
            LoadTreeView();
        }

        private List<FilterCondition> cellMorphology;
        private List<FilterCondition> immunohistochemistry;
        private List<FilterCondition> organizationalStructure;
        private List<FilterCondition> interstitium;
        private List<FilterCondition> bloodVessel;

        List<int> checkedConditions;



        private async void LoadTreeView()
        {
            uiFlowLayoutPanelResults.Clear();

            var allCondition = await services.LoadAllConditionAsync();


            uiComboBox1.Items.Clear();
            uiComboBox2.Items.Clear();
            uiComboBox3.Items.Clear();
            uiComboBox4.Items.Clear();
            uiComboBox5.Items.Clear();

            uiTreeView1.Nodes.Clear();
            uiTreeView2.Nodes.Clear();
            uiTreeView3.Nodes.Clear();
            uiTreeView4.Nodes.Clear();
            uiTreeView5.Nodes.Clear();




            cellMorphology = allCondition
                 .Where(f => f.Filter == Filter.CellMorphologyFilter)
                 .ToList();
            foreach (FilterCondition condition in cellMorphology)
            {
                TreeNode treeNode = new TreeNode();

                treeNode.Tag = condition.Id;
                treeNode.Text = condition.ToString();
                treeNode.Name = condition.ToString();

                uiTreeView1.Nodes.Add(treeNode);
                uiComboBox1.Items.Add(condition);
            }

            immunohistochemistry = allCondition
               .Where(f => f.Filter == Filter.ImmunohistochemistryFilte)
               .ToList();
            foreach (FilterCondition condition in immunohistochemistry)
            {
                TreeNode treeNode = new TreeNode();

                treeNode.Tag = condition.Id;
                treeNode.Text = condition.ToString();
                treeNode.Name = condition.ToString();

                uiTreeView2.Nodes.Add(treeNode);
                uiComboBox2.Items.Add(condition);
            }


            organizationalStructure = allCondition
              .Where(f => f.Filter == Filter.OrganizationalStructureFilter)
              .ToList();
            foreach (FilterCondition condition in organizationalStructure)
            {
                TreeNode treeNode = new TreeNode();

                treeNode.Tag = condition.Id;
                treeNode.Text = condition.ToString();
                treeNode.Name = condition.ToString();

                uiTreeView3.Nodes.Add(treeNode);
                uiComboBox3.Items.Add(condition);
            }


            interstitium = allCondition
               .Where(f => f.Filter == Filter.InterstitiumFilter)
               .ToList();
            foreach (FilterCondition condition in interstitium)
            {
                TreeNode treeNode = new TreeNode();

                treeNode.Tag = condition.Id;
                treeNode.Text = condition.ToString();
                treeNode.Name = condition.ToString();

                uiTreeView4.Nodes.Add(treeNode);
                uiComboBox4.Items.Add(condition);
            }


            bloodVessel = allCondition
               .Where(f => f.Filter == Filter.BloodVesselFilter)
               .ToList();
            foreach (FilterCondition condition in bloodVessel)
            {
                TreeNode treeNode = new TreeNode();

                treeNode.Tag = condition.Id;
                treeNode.Text = condition.ToString();
                treeNode.Name = condition.ToString();

                uiTreeView5.Nodes.Add(treeNode);
                uiComboBox5.Items.Add(condition);
            }



        }



     

        private void DisplayResult(List<CalculateResult> calculateResult)
        {
            uiFlowLayoutPanelResults.Clear();
            foreach (CalculateResult result in calculateResult)
            {
                CalculateResultBox calculateResultBox = new CalculateResultBox(result);
                calculateResultBox.SetDPIScale();
                uiFlowLayoutPanelResults.Add(calculateResultBox);
            }
        }









        private void uiComboBox_TextChanged(object sender, EventArgs e)
        {
            UIComboBox comboBox = (UIComboBox)sender;

            int index = Convert.ToInt32(comboBox.Name.Substring(comboBox.Name.Length - 1));

            string indexString = comboBox.Text;
            switch (index)
            {
                case 1:
                    {

                        if (!string.IsNullOrEmpty(indexString))
                        {
                            int count = uiTreeView1.Nodes.Count;
                            int comboxIndex = cellMorphology.FindIndex(f => f.Name == indexString);
                            if (count - comboxIndex <= 5)
                            {
                                uiTreeView1.Nodes[comboxIndex].EnsureVisible();
                            }
                            else
                            {
                                uiTreeView1.Nodes[comboxIndex + 5].EnsureVisible();
                            }
                        }

                    }

                    break;
                case 2:
                    {
                        if (!string.IsNullOrEmpty(indexString))
                        {
                            int count = uiTreeView2.Nodes.Count;
                            int comboxIndex = immunohistochemistry.FindIndex(f => f.Name == indexString);
                            if (count - comboxIndex <= 5)
                            {
                                uiTreeView2.Nodes[comboxIndex].EnsureVisible();
                            }
                            else
                            {
                                uiTreeView2.Nodes[comboxIndex + 5].EnsureVisible();
                            }
                        }
                    }
                    break;
                case 3:
                    {
                        if (!string.IsNullOrEmpty(indexString))
                        {
                            int count = uiTreeView3.Nodes.Count;
                            int comboxIndex = organizationalStructure.FindIndex(f => f.Name == indexString);
                            if (count - comboxIndex <= 5)
                            {
                                uiTreeView3.Nodes[comboxIndex].EnsureVisible();
                            }
                            else
                            {
                                uiTreeView3.Nodes[comboxIndex + 5].EnsureVisible();
                            }
                        }
                    }
                    break;
                case 4:
                    {
                        if (!string.IsNullOrEmpty(indexString))
                        {
                            int count = uiTreeView4.Nodes.Count;
                            int comboxIndex = interstitium.FindIndex(f => f.Name == indexString);
                            if (count - comboxIndex <= 5)
                            {
                                uiTreeView4.Nodes[comboxIndex].EnsureVisible();
                            }
                            else
                            {
                                uiTreeView4.Nodes[comboxIndex + 5].EnsureVisible();
                            }
                        }

                    }
                    break;
                case 5:
                    {
                        if (!string.IsNullOrEmpty(indexString))
                        {
                            int count = uiTreeView5.Nodes.Count;
                            int comboxIndex = bloodVessel.FindIndex(f => f.Name == indexString);
                            if (count - comboxIndex <= 5)
                            {
                                uiTreeView5.Nodes[comboxIndex].EnsureVisible();
                            }
                            else
                            {
                                uiTreeView5.Nodes[comboxIndex + 5].EnsureVisible();
                            }
                        }
                    }
                    break;
                default: break;

            }





        }

        private void uiSymbolButtonClear_Click(object sender, EventArgs e)
        {
            UISymbolButton button = (UISymbolButton)sender;
            if (null == button)
            {
                return;
            }
            int index = Convert.ToInt32(button.Name.Substring(button.Name.Length - 1));
            switch (index)
            {
                case 1:
                    uiTreeView1.UnCheckedAll();
                    break;
                case 2:
                    uiTreeView2.UnCheckedAll();
                    break;
                case 3:
                    uiTreeView3.UnCheckedAll();
                    break;
                case 4:
                    uiTreeView4.UnCheckedAll();
                    break;
                case 5:
                    uiTreeView5.UnCheckedAll();
                    break;
                default: break;

            }


        }

        private void uiTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
           TreeNode node = e.Node ;
            if (null != node)
            {
                int id = int.Parse(node.Tag.ToString());
                if (node.Checked)
                {
                    checkedConditions.Add(id);
                }
                else
                {
                    if (checkedConditions.Contains(id))
                    {
                        checkedConditions.Remove(id);
                    }
                }
            }
            QueryResult();
        }


        private async void QueryResult()
        {

            int a = 1;

            var result =  await services.QueryAnsy(checkedConditions);
            DisplayResult(result);
        }

    }
}
